package com.bancomer.batch.converters;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import com.bancomer.batch.converters.impl.AuditAlertImpl;
import com.bancomer.batch.pojo.Detail;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.Summary;
import com.blackbear.flatworm.ConfigurationReader;
import com.blackbear.flatworm.FileFormat;
import com.blackbear.flatworm.MatchedRecord;

public class FlatWormReader {
	
	public static AuditAlert readSummary(Properties alertConstants, FileHelper summaryFileScript) throws Exception{
		AuditAlert auditAlert = new AuditAlertImpl();
		ConfigurationReader parser = new ConfigurationReader();
		FileFormat fileFormat = parser.loadConfigurationFile(summaryFileScript.getProperties().getProperty(alertConstants.getProperty("CONVERTER_PATH_CONFIG")));
        InputStream inputStream = new FileInputStream(summaryFileScript.getLocalPath()+summaryFileScript.getName()+System.getProperty("file.separator")+"part-r-00000");
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(inputStream));
        MatchedRecord results;
        while ((results = fileFormat.getNextRecord(bufIn)) != null) {
            if (results.getRecordName().equals("TSALAR")) {
               Summary summary = (Summary)results.getBean("resumen");
               auditAlert.addSummary(summary);
            }
        }
        return auditAlert;
	}
	public static AuditAlert readDetail(Properties alertConstants, FileHelper detailFileScript) throws Exception{
		AuditAlert auditAlert = new AuditAlertImpl();
		ConfigurationReader parser = new ConfigurationReader();
		FileFormat fileFormat = parser.loadConfigurationFile(detailFileScript.getProperties().getProperty(alertConstants.getProperty("CONVERTER_PATH_CONFIG")));
        InputStream inputStream = new FileInputStream(detailFileScript.getLocalPath()+detailFileScript.getName()+System.getProperty("file.separator")+"part-r-00000");
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(inputStream));
        MatchedRecord results;
        while ((results = fileFormat.getNextRecord(bufIn)) != null) {
            if (results.getRecordName().equals("TSALAD")) {
               Detail detail = (Detail)results.getBean("desglose");
               auditAlert.addDetail(detail);
            }
        }
        return auditAlert;
	}
}
